package com.apes.enquiry.orderForm.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.rbac.model.User;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = OrderForm.CONST_TABLE_NAME)
public class OrderForm extends Model {
    public static final String CONST_TABLE_NAME = "sp_order_form";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "订单", padding = 10)
    @Comment("订单号")
    private String id; //订单号


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商")
    private PartyRole supplier;

    @Column(name = "client", length = 100)
    @Comment("门店")
    private String client;//客户

    @Column(name = "client_id", length =10)
    @Comment("门店编码")
    private String clientId;

    @Column(name = "address", length = 100)
    @Comment("配送地址")
    private String address;

    @Column(name = "relation", length = 50)
    @Comment("联系人")
    private String relation;

    @Column(name = "phone", length = 20)
    @Comment("联系电话")
    private String phone;

    @Column(name = "affirmperson", length = 20)
    @Comment("确认人")
    private String affirmPerson;

    @OneToMany(mappedBy = "orderForm", cascade = CascadeType.ALL, orphanRemoval = true)
    @Comment("订单明细")
    private List<OrderFormDetail> detailList = new ArrayList<>();

    public List<OrderFormDetail> getDetailList() {
        if(detailList == null) return new ArrayList<>();
        for (OrderFormDetail orderFormDetail : detailList) {
            orderFormDetail.setOrderForm(this);
        }
        return this.detailList;
    }

    @Column(name = "related_number", length = 24)
    @Comment("要货单")
    private String relatedNumber;//要货单

    @Column(name = "source", length = 30)
    @Comment("服务方案单")
    private String source;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delivery_date")
    @Comment("发货时间")
    private Date deliveryDate;

    @Column(name = "invoicing", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否开票")
    private boolean invoicing;//（0：不开票  1：开票）默认不开票

    @States(name = "单据状态", values = {
            @State(id = "receipt", name = "待收货"),
            @State(id = "deliver", name = "待发货"),
            @State(id = "pending", name = "待确认"),
            @State(id = "cancel", name = "已取消"),
            @State(id = "finish", name = "完成")
    })
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Column(name = "VIN", length = 20)
    @Comment("车架号")
    private String VIN;

    @Column(name = "car_license", length = 64)
    @Comment("车牌号码")
    private String carLicense;

    @Column(name = "vehicle_configuration", length = 20)
    @Comment("车型配置ID")
    private String vehicleConfiguration;

    @Column(name = "money", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("订单金额")
    private Double money;

    @Column(name = "supplier_number", length = 20)
    @Comment("第三方单号")
    private String supplierNumber;

    @Column(name = "supplier_name", length = 20)
    @Comment("第三方单号")
    private String supplierName;

    @Column(name = "note", length = 128)
    @Comment("异常原因")
    private String note;

    public  Double getMoney(){
        this.money=0.0;
        if(detailList==null || detailList.size()==0)return this.money;
        for (OrderFormDetail detail: detailList) {
            this.money+=detail.getOrderQuantity()*detail.getTaxUnitPrice();
        }
        return  Double.parseDouble(String.format("%.2f", (this.money)));
    }


    @Transient
    private Integer quantity;
    public  Integer getQuantity(){
        if(detailList==null || detailList.size()==0)return 0;
        return this.detailList.size();
    }

    @Transient
    private String partyName;
    public  String getPartyName(){
        if(supplier.getParty()!=null){
            return this.supplier.getParty().getName();
        }
        return  null;
    }

    @Transient
    private String offerGroup;








}
